package com.gemme.gemme.wode;

import android.content.Intent;
import android.os.Bundle;
import android.text.Spannable;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.view.Topview;
import com.google.android.material.tabs.TabLayout;
import com.hyphenate.chat.ChatClient;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMConversation;
import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.utils.EaseCommonUtils;
import com.hyphenate.easeui.utils.EaseSmileUtils;
import com.hyphenate.helpdesk.easeui.util.IntentBuilder;
import com.jaeger.library.StatusBarUtil;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

//侧滑删除地址
public class Message2Activity extends BaseActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message2);
        initTop();
        initView();
        initChatData();
    }


    TextView tv_chat_desc;
    private void initView() {
        View ll_zan = findViewById(R.id.ll_zan);
        View ll_pinglun = findViewById(R.id.ll_pinglun);
        View ll_wuliu = findViewById(R.id.ll_wuliu);
        View ll_msrx = findViewById(R.id.ll_msrx);
        View ll_kefu = findViewById(R.id.ll_kefu);
        View ll_findMessage = findViewById(R.id.ll_findMessage);
        tv_chat_desc = findViewById(R.id.tv_chat_desc);
        ll_zan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext,Zan2Activity.class));
            }
        });

        ll_findMessage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext,FindMessageActivity.class));
            }
        });

        ll_pinglun.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext,PinglunActivity.class));
            }
        });

        ll_wuliu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext,DingdanMessageActivity.class));
            }
        });

        ll_msrx.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(mContext,ChatMessage2Activity.class));
            }
        });
        //客服
        ll_kefu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(ChatClient.getInstance().isLoggedInBefore()){
                    //已经登录，可以直接进入会话界面
                    //zhaoweichen  gemmekefu

                    System.out.println();
                    Intent intent = new IntentBuilder(mContext)
                            .setServiceIMNumber("gemmekefu") //获取地址：kefu.easemob.com，“管理员模式 > 渠道管理 > 手机APP”页面的关联的“IM服务号”
                            .build();
                    startActivity(intent);
                }else{
                    //未登录，需要登录后，再进入会话界面
                    System.out.println();
                }

            }
        });
    }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("消息");
    }

    private void initChatData() {
       try {
           List<EMConversation> emConversations = loadConversationList();
           String name = emConversations.get(0).conversationId();
           EMMessage lastMessage = emConversations.get(0).getLastMessage();
           Spannable message = EaseSmileUtils.getSmiledText(this, EaseCommonUtils.getMessageDigest(lastMessage, (this)));
           System.out.println("");
//           tv_chat_desc.setText("mole" + ":"+  "嗨，在吗？");
           tv_chat_desc.setText(name + ":"+  message);
       }catch (Exception e){

       }
    }

    protected List<EMConversation> loadConversationList(){
        // get all conversations
        Map<String, EMConversation> conversations = EMClient.getInstance().chatManager().getAllConversations();
        List<Pair<Long, EMConversation>> sortList = new ArrayList<Pair<Long, EMConversation>>();
        /**
         * lastMsgTime will change if there is new message during sorting
         * so use synchronized to make sure timestamp of last message won't change.
         */
        synchronized (conversations) {
            for (EMConversation conversation : conversations.values()) {
                if (conversation.getAllMessages().size() != 0) {
                    sortList.add(new Pair<Long, EMConversation>(conversation.getLastMessage().getMsgTime(), conversation));
                }
            }
        }
        try {
            // Internal is TimSort algorithm, has bug
            sortConversationByLastChatTime(sortList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<EMConversation> list = new ArrayList<EMConversation>();
        for (Pair<Long, EMConversation> sortItem : sortList) {
            list.add(sortItem.second);
        }
        return list;
    }

    /**
     * sort conversations according time stamp of last message
     *
     * @param conversationList
     */
    private void sortConversationByLastChatTime(List<Pair<Long, EMConversation>> conversationList) {
        Collections.sort(conversationList, new Comparator<Pair<Long, EMConversation>>() {
            @Override
            public int compare(final Pair<Long, EMConversation> con1, final Pair<Long, EMConversation> con2) {

                if (con1.first.equals(con2.first)) {
                    return 0;
                } else if (con2.first.longValue() > con1.first.longValue()) {
                    return 1;
                } else {
                    return -1;
                }
            }

        });
    }



}